﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using BitPortal.Utils;
namespace BitPortal.Business
{
    public static partial class StatisticBLL
    {
        public static int UpdateView(long staticID, int addWith = 1, bool registerIpAddress = true, bool registerUserID = true)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Statistic st = bitEnt.Statistics.SingleOrDefault(ent => ent.StatisticID == staticID);
            if (st == null) return 0;
            st.ViewCount += addWith;
            if (registerIpAddress)
            {
                st.ViewUserIPs += HttpContext.Current.Request.UserHostAddress + "#";
            }
            if (registerUserID && BitContext.Current.LoginUser != null)
            {
                st.ViewUserIDs += BitContext.Current.LoginUser.ToString() + "#";
            }
            return bitEnt.SaveChanges();
        }

        public static int AutoUpdateView(long staticID)
        {
            return UpdateView(staticID, 1, (bool)PortalConfigBLL.GetValueByName("Statistic.Statistic.RegisterIPView"), (bool)PortalConfigBLL.GetValueByName("Statistic.Statistic.RegisterUserIDView"));
        }

        public static Statistic UpdateLike(long staticID, int value = 1, bool checkCookie = true, bool checkID = true)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Statistic st = bitEnt.Statistics.SingleOrDefault(ent => ent.StatisticID == staticID);
            if (st == null) return null;

            #region
            int check = 0;
            //check cookie
            if (checkCookie)
            {
                check = GetCookieValue(staticID);
            }
            //check ID
            if (checkID)
            {
                if (BitContext.Current.LoginUser == null) throw new Exception("Bạn chưa đăng nhập");
                if ((st.LikeUserIDs).Contains("#" + BitContext.Current.LoginUser.ToString() + "#"))
                {
                    check = 1;
                }
                else if (st.DislikeUserIDs.Contains("#" + BitContext.Current.LoginUser.ToString() + "#"))
                {
                    check = -1;
                }
            }

            #endregion

            #region
            if (check == 0)
            {
                st.LikeCount += (value > 0) ? value : 0;
                st.DislikeCount -= (value < 0) ? value : 0;
            }
            else
            {
                if (value == check)
                {
                    throw new Exception("Bạn đã " + (check == 1 ? "thích" : "không thích") + " trước đó!");
                }
                st.LikeCount += value;
                st.DislikeCount -= value;
            }

            SetCookieValue(staticID, value);
            if (BitContext.Current.LoginUser != null)
            {
                if (value > 0)
                {
                    st.LikeUserIDs += BitContext.Current.LoginUser.ToString() + "#";
                    st.DislikeUserIDs.Replace("#" + BitContext.Current.LoginUser.ToString() + "#", "#");
                }
                else
                {
                    st.DislikeUserIDs += BitContext.Current.LoginUser.ToString() + "#";
                    st.LikeUserIDs.Replace("#" + BitContext.Current.LoginUser.ToString() + "#", "#");
                }
            }
            #endregion

            bitEnt.SaveChanges();
            return st;
        }

        public static int GetCookieValue(long statisticID)
        {
            try
            {
                return GeneralUtils.ToInt32TryParse(HttpContext.Current.Request.Cookies["Statistic"].Values[statisticID.ToString()]);
            }
            catch { return 0; }
        }

        public static void SetCookieValue(long statisticID, int value)
        {
            HttpContext.Current.Response.Cookies["Statistic"].Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.Cookies["Statistic"].Values[statisticID.ToString()] = value.ToString();
        }

        public static Statistic GetByID(long statisticID)
        {
            return (new ExtendedBitPortalEntities()).Statistics.SingleOrDefault(ent => ent.StatisticID == statisticID);
        }
    }
}